#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'walkingsky'

from models.huoban_api import HuobanApi

def save_stock_holding_ranking():
    '''
        从伙伴获取股票持仓排行，并写入csv文件，用来导入东方财富终端的“导入自选”功能
    '''
    from config.env_config import DATA_FILE_PATH

    huoban = HuobanApi('dong.jack.999@gmail.com','qwer0987','4000000003068432')
    huoban.login()

    res_json = huoban.get_item_list()
    # print(res)
    # res_json = json.loads(res)
    reslt_st = ''
    for item in res_json['data']['items']:
        # print(item)
        if len(item) >= 18:
            code_split = item[17].split('.')
            if code_split[0] == '0' :
                code_str = code_split[1] + '.SZ'
            elif code_split[0] == '1' :
                code_str = code_split[1] + '.SH'
            else:
                code_str = code_split[1] + '.HK'
            reslt_st = reslt_st + code_split[1] + ','  + "\n"
            print(code_split[1] + ','  +item[16])

    # print(reslt_st)
    with open(DATA_FILE_PATH + "../自选股票.csv", "w", encoding="utf-8") as file:
        file.write(reslt_st)
    

if __name__ == "__main__":
    save_stock_holding_ranking()